<script setup>
import { artGetChannelsService, artDelChannelService } from '@/api/article.js'
import { Edit, Delete } from '@element-plus/icons-vue';
import { ref } from 'vue'
import channelEdit from './components/channelEdit.vue';
import { i18n } from "@/i18n/index.js";
const { t } = i18n.global;


const channelList = ref([])
const loading = ref(false)
const dialog = ref()
const getChannelList = async () => {
  loading.value = true
  const res = await artGetChannelsService()
  channelList.value = res.data.data
  loading.value = false
}

getChannelList()

const onDelChannel = async (row) => {
  await ElMessageBox.confirm(t('message.ConfirmDelete'), t('message.WarmReminder'), {
    type: 'warning',
    confirmButtonText: t('message.Confirm'), // 替换为 i18n 语法
    cancelButtonText: t('message.Cancel')
  })
  await artDelChannelService(row.id)
  ElMessage({ type: 'success', message: t('message.SuccessDelete') })
  getChannelList()
}
const onEditChannel = (row) => {
  dialog.value.open(row)
}
const onAddChannel = () => {
  dialog.value.open({})
}
const onSuccess = () => {
  getChannelList()
}

</script>

<template>
  <page-container :title="$t('message.ArticleCategory')">
    <template #extra>
      <el-button @click="onAddChannel">{{ $t('message.AddCategory') }}</el-button>
    </template>
    <!-- 主体部分，是表格 -->
    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column :label="$t('message.CategoryName')" width="100" type="index"> </el-table-column>
      <el-table-column :label="$t('message.CategoryName')" prop="cate_name"></el-table-column>
      <el-table-column :label="$t('message.CategoryAlias')" prop="cate_alias"></el-table-column>
      <el-table-column :label="$t('message.Actions')" width="100">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="onEditChannel(row)"></el-button>
          <el-button :icon="Delete" circle plain type="danger" @click="onDelChannel(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty :description="$t('message.NoData')" />
      </template>
    </el-table>

    <channelEdit ref="dialog" @success="onSuccess"></channelEdit>
  </page-container>
</template>

<style lang="scss" scoped></style>
